package cn.ticket.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.ticket.dao.WorkbusesMapper;
import cn.ticket.model.Workbuses;
import cn.ticket.service.WorkBusesService;

@Service
public class WorkBusesServcieImpl implements WorkBusesService {

	@Autowired
	private WorkbusesMapper workBusesMapper;

	@Override
	public int deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return workBusesMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Workbuses record) {
		// TODO Auto-generated method stub
		return workBusesMapper.insert(record);
	}

	@Override
	public int insertSelective(Workbuses record) {
		// TODO Auto-generated method stub
		return workBusesMapper.insertSelective(record);
	}

	@Override
	public Workbuses selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return workBusesMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(Workbuses record) {
		// TODO Auto-generated method stub
		return workBusesMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Workbuses record) {
		// TODO Auto-generated method stub
		return workBusesMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<Workbuses> getValidate0() {
		// TODO Auto-generated method stub
		Date startDate = new Date();
		Date endDate = new Date(startDate.getTime() + 1000 * 60 * 60 * 24 * 3);
		return workBusesMapper.getByDateAndOrientation(0, startDate, endDate);
	}

	@Override
	public List<Workbuses> getValidate1() {
		Date startDate = new Date();
		Date endDate = new Date(startDate.getTime() + 1000 * 60 * 60 * 24 * 3);
		return workBusesMapper.getByDateAndOrientation(1, startDate, endDate);
	}

	@Override
	public List<Workbuses> getInValidate() {
		// TODO Auto-generated method stub
		Date startDate = new Date();
		Date endDate = new Date(startDate.getTime() - 1000 * 60 * 60 * 24 * 30);
		return workBusesMapper.getByDate(startDate, endDate);
	}

	@Override
	public List<Map<String, Object>> getByCount(Integer start, Integer count) {
		// TODO Auto-generated method stub
		return workBusesMapper.getByCount(start, count);
	}

	@Override
	public List<Workbuses> getValidate() {
		Date startDate = new Date();
		Date endDate = new Date(startDate.getTime() + 1000 * 60 * 60 * 24 * 15);
		System.err.println(startDate + "-时间段-" + endDate);
		return workBusesMapper.getByDate(startDate, endDate);
	}

	@Override
	public List<Map<String, Object>> getInfoByDateAndOrientation(String orientation, Date startDate, Date endDate) {
		// TODO Auto-generated method stub
		return workBusesMapper.getInfoByDateAndOrientation(orientation, startDate, endDate);
	}

	@Override
	public boolean generateWorkBuses(String today) throws ParseException {
		// TODO Auto-generated method stub
		// String today="";
		boolean isFriday = false;
		Calendar c = Calendar.getInstance();
		int weekday = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (weekday > 1 && weekday < 5) {

		} else {
			isFriday = true;
		}

		if (!isFriday) {
			Workbuses workbuses1 = new Workbuses();
			workbuses1.setRouteid((short) 20);
			workbuses1.setDeparturedate(today + " 8:40:00");
			workbuses1.setEndbusdate(today + " 8:40:00");

			Workbuses workbuses2 = new Workbuses();
			workbuses2.setRouteid((short) 21);
			workbuses2.setDeparturedate(today + " 12:30:00");
			workbuses2.setEndbusdate(today + " 12:30:00");

			Workbuses workbuses21 = new Workbuses();
			workbuses21.setRouteid((short) 26);
			workbuses21.setDeparturedate(today + " 13:00:00");
			workbuses21.setEndbusdate(today + " 13:00:00");

			Workbuses workbuses22 = new Workbuses();
			workbuses22.setRouteid((short) 27);
			workbuses22.setDeparturedate(today + " 17:30:00");
			workbuses22.setEndbusdate(today + " 17:30:00");

			Workbuses workbuses3 = new Workbuses();
			workbuses3.setRouteid((short) 22);
			workbuses3.setDeparturedate(today + " 17:30:00");
			workbuses3.setEndbusdate(today + " 17:30:00");

			Workbuses workbuses4 = new Workbuses();
			workbuses4.setRouteid((short) 24);
			workbuses4.setDeparturedate(today + " 20:00:00");
			workbuses4.setEndbusdate(today + " 20:00:00");

			insertSelective(workbuses1);
			insertSelective(workbuses2);
			insertSelective(workbuses21);
			insertSelective(workbuses22);
			insertSelective(workbuses3);
			insertSelective(workbuses4);
		} else {
			Workbuses workbuses5 = new Workbuses();
			workbuses5.setRouteid((short) 20);
			workbuses5.setDeparturedate(today + " 8:40:00");
			workbuses5.setEndbusdate(today + " 8:40:00");

			Workbuses workbuses6 = new Workbuses();
			workbuses6.setRouteid((short) 25);
			workbuses6.setDeparturedate(today + " 16:30:00");
			workbuses6.setEndbusdate(today + " 16:30:00");
			insertSelective(workbuses5);
			insertSelective(workbuses6);
		}
		return true;
	}

	@Override
	public boolean generateWorkBuses() throws ParseException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean generateWorkBuses(Date date) throws ParseException {

		boolean isFriday = false;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int weekday = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (weekday > 0 && weekday < 5) {

		} else if (weekday == 5) {
			isFriday = true;
		} else if (weekday == 6) {
			date = new Date(date.getTime() + (long)1000 * 60 * 60 * 24 * 2);
		} else if (weekday == 0) {
			date = new Date(date.getTime() + (long)1000 * 60 * 60 * 24 * 2);
		} else {
			return false;
		}
		// 随机睡眠一段时间 让各服务器不同时生成班车 可以检测
		Random rand = new Random(2);
		Long delayMilis = (long) rand.nextInt()*1000;
		try {
			Thread.sleep(delayMilis);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Date endDate = new Date(date.getTime() + 1000 * 60 * 60 * 24);
		List<Workbuses> workbuses = workBusesMapper.getByDate(date, endDate);
		if (!workbuses.isEmpty()) {
			return false;
		}

		String today = new SimpleDateFormat("yyyy-MM-dd").format(date);
		if (!isFriday) {
			Workbuses workbuses1 = new Workbuses();
			workbuses1.setRouteid((short) 20);
			workbuses1.setDeparturedate(today + " 8:40:00");
			workbuses1.setEndbusdate(today + " 8:40:00");

			Workbuses workbuses2 = new Workbuses();
			workbuses2.setRouteid((short) 21);
			workbuses2.setDeparturedate(today + " 12:30:00");
			workbuses2.setEndbusdate(today + " 12:30:00");

			Workbuses workbuses21 = new Workbuses();
			workbuses21.setRouteid((short) 26);
			workbuses21.setDeparturedate(today + " 13:00:00");
			workbuses21.setEndbusdate(today + " 13:00:00");

			Workbuses workbuses22 = new Workbuses();
			workbuses22.setRouteid((short) 27);
			workbuses22.setDeparturedate(today + " 17:30:00");
			workbuses22.setEndbusdate(today + " 17:30:00");

			Workbuses workbuses3 = new Workbuses();
			workbuses3.setRouteid((short) 22);
			workbuses3.setDeparturedate(today + " 17:30:00");
			workbuses3.setEndbusdate(today + " 17:30:00");

			Workbuses workbuses4 = new Workbuses();
			workbuses4.setRouteid((short) 24);
			workbuses4.setDeparturedate(today + " 20:00:00");
			workbuses4.setEndbusdate(today + " 20:00:00");

			insertSelective(workbuses1);
			insertSelective(workbuses2);
			insertSelective(workbuses21);
			insertSelective(workbuses22);
			insertSelective(workbuses3);
			insertSelective(workbuses4);
		} else {
			Workbuses workbuses5 = new Workbuses();
			workbuses5.setRouteid((short) 20);
			workbuses5.setDeparturedate(today + " 8:40:00");
			workbuses5.setEndbusdate(today + " 8:40:00");

			Workbuses workbuses6 = new Workbuses();
			workbuses6.setRouteid((short) 25);
			workbuses6.setDeparturedate(today + " 16:30:00");
			workbuses6.setEndbusdate(today + " 16:30:00");
			insertSelective(workbuses5);
			insertSelective(workbuses6);
		}
		return true;
	}

	@Override
	public List<Map<String, Object>> getInfoByDate(Date startDate, Date endDate) {
		// TODO Auto-generated method stub
		return workBusesMapper.getInfoByDate(startDate, endDate);
	}

	@Override
	public boolean generateWorkBusesNoAudit(String today) throws ParseException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean generateWorkBusesNoAudit(Date date) throws ParseException {
		// TODO Auto-generated method stub
		Random rand = new Random(2);
		Long delayMilis = (long) rand.nextInt(10000);
		try {
			Thread.sleep(delayMilis);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int weekday = c.get(Calendar.DAY_OF_WEEK) - 1;
		Date tomorrowDate = new Date(date.getTime() + (long)1000 * 60 * 60 * 24);
	
		if (weekday > 0 && weekday < 5) {
			String today = new SimpleDateFormat("yyyy-MM-dd").format(date);
			Workbuses workbuses1 = new Workbuses();
			workbuses1.setRouteid((short) 23);
			workbuses1.setSeatscount((short) 47);
			workbuses1.setIsneedaudit(false);
			workbuses1.setDeparturedate(today + " 17:10:00");
			workbuses1.setValiditydate(today + " 12:30:00");
			workbuses1.setEndbusdate(today + " 16:30:00");
			
			String tomorrow = new SimpleDateFormat("yyyy-MM-dd").format(tomorrowDate);
			Workbuses workbuses2 = new Workbuses();
			workbuses2.setRouteid((short) 28);
			workbuses2.setSeatscount((short) 47);
			workbuses2.setIsneedaudit(false);
			workbuses2.setDeparturedate(tomorrow + " 06:50:00");
			workbuses2.setValiditydate(today + " 12:30:00");
			workbuses2.setEndbusdate(today + " 16:30:00");

			List<Workbuses> workbuses = workBusesMapper.getByDate(new Date(date.getTime()+(long)1000*60*60*4), new Date(tomorrowDate.getTime()+(long)1000 * 60 * 60 * 24*30));
			if (!workbuses.isEmpty()) {
				return false;
			}
			
			insertSelective(workbuses1);
			insertSelective(workbuses2);
			
			Workbuses workbuses3 = new Workbuses();
			workbuses3.setRouteid((short)21);
			workbuses3.setSeatscount((short) 47);
			workbuses3.setIsneedaudit(false);
			workbuses3.setDeparturedate(today + " 17:10:00");
			workbuses3.setValiditydate(today + " 12:30:00");
			workbuses3.setEndbusdate(today + " 16:30:00");
			
			Workbuses workbuses4 = new Workbuses();
			workbuses4.setRouteid((short) 29);
			workbuses4.setSeatscount((short) 47);
			workbuses4.setIsneedaudit(false);
			workbuses4.setDeparturedate(tomorrow + " 06:50:00");
			workbuses4.setValiditydate(today + " 12:30:00");
			workbuses4.setEndbusdate(today + " 16:30:00");
			
			insertSelective(workbuses3);
			insertSelective(workbuses4);
		}
		return true;
	}

	@Override
	public List<Workbuses> getByDepartureDate(Date departureDate) {
		// TODO Auto-generated method stub
		return workBusesMapper.getByDepartureDate(departureDate);
	}

	@Override
	public int seatOccupationAdd1(int id) {
		// TODO Auto-generated method stub
		return workBusesMapper.seatOccupationAdd1(id);
	}

	@Override
	public int seatOccupationRemove1(int id) {
		// TODO Auto-generated method stub
		return workBusesMapper.seatOccupationRemove1(id);
	}

	@Override
	public int seatOccupationAuditedAdd1(int id) {
		// TODO Auto-generated method stub
		return workBusesMapper.seatOccupationAuditedAdd1(id);
	}

	@Override
	public int seatOccupationAuditedRemove1(int id) {
		// TODO Auto-generated method stub
		return workBusesMapper.seatOccupationAuditedRemove1(id);
	}

}
